var EXPORTED_SYMBOLS = ["no3rdPartyUtil","no3rdPartyLogger","no3rdPartyPreferences"];

no3rdPartyUtil = {
	eTLDService : Components.classes["@mozilla.org/network/effective-tld-service;1"].getService(Components.interfaces.nsIEffectiveTLDService),
	getTLD : function (hostname) {
		//var parts = hostname.split('.');
		//var slice = (parts[parts.length-2].length==2)?parts.length-3: parts.length-2;
		//return parts.slice(slice).join('.');
		return this.eTLDService.getBaseDomainFromHost(hostname); 
	},

	matchDomains : function(originatingPageHost, channelDomain) {
		var origTLD = this.getTLD(originatingPageHost);
		var channelTLD = this.getTLD(channelDomain);
		return (origTLD == channelTLD) ? true : false;
 	},

}

// Logger
var no3rdPartyLogger =
{ 	
	debug: function(message)
  	{
   		this.writeFile("debug.log", message + "\n");
  	},

	log: function(message)
  	{
   		this.debug(message);
		this.writeFile("no3rdParty.log", message + "\n");
  	},

	writeFile : function (filename,message) {
		var outFile = no3rdPartyExtensionDir.clone();
		outFile.append(filename)
		if(outFile.exists() == false)
 		{
 			outFile.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420);
 		}
		//create file output stream and use write/create/truncate mode
		//0x02 writing, 0x08 create file, 0x20 truncate, length if exist, 0x10 append
		var stream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
		stream.init(outFile, 0x02 | 0x08 | 0x10, 0666, 0);

		//write data to file then close output stream
			
		stream.write(message, message.length);
		stream.close();

		},
}


var no3rdPartyPreferences = function() {
	mozPrefs: null,
	
	_server: null,
	_shouldRun: null,
	_shouldUpload: null,
	_googleUser: null,
	_uniqueID: null,
	_reportDeleteCancel: null,
	_storeLocally: null,
	_maxLocalStorage: null,
	_highlight3rdPartyResources: null,
	_displayStatusCount: null,

			
	init: function() 
	{
		no3rdPartyLogger.debug("Initializing Preferences");
		// Register to receive notifications of preference changes
		this.mozPrefs = Components.classes["@mozilla.org/preferences-service;1"]
				.getService(Components.interfaces.nsIPrefService)
				.getBranch("extensions.no3rdparty.");
		this.mozPrefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
		this.mozPrefs.addObserver("", this, false);
	
		// init values

		this._server = this.mozPrefs.getCharPref("server");
		this._shouldRun = this.mozPrefs.getBoolPref("shouldRun");
		this._shouldUpload = this.mozPrefs.getBoolPref("shouldUpload");
		this._googleUser = this.mozPrefs.getCharPref("googleUsername");
		this._uniqueID = this.mozPrefs.getCharPref("uniqueID");
		if (! this._uniqueID ) {
			this.uniqueID = this.generateUniqueID();
		}
		this._reportDeleteCancel = this.mozPrefs.getCharPref("reportDeleteCancel");
		this._storeLocally = this.mozPrefs.getBoolPref("storeLocally");
		this._maxLocalStorage = this.mozPrefs.getIntPref("maxLocalStorage");

		no3rdPartyLogger.debug(no3rdPartyPreferences.toString());
	},
	
	shutdown: function()
	{
		this.mozPrefs.removeObserver("", this);
	},
	
	observe: function(subject, topic, data)
	{
		if (topic != "nsPref:changed")
		{
			return;
		}

		switch(data)
		{
			case "server":
				this._server = this.mozPrefs.getCharPref("server");
				break;
				
			case "shouldRun":
				this._shouldRun = this.mozPrefs.getBoolPref("shouldRun");
				break;
				
			case "shouldUpload":
				this._shouldUpload = this.mozPrefs.getBoolPref("shouldUpload");
				break;
				
			case "googleUsername":
				this._ColorRequests = this.mozPrefs.getCharPref("googleUsername");
				break;
				
			case "uniqueID":
				this._uniqueID = this.mozPrefs.getCharPref("uniqueID");
				if (! this._uniqueID) {
					this._uniqueID = this.generateUniqueID();
				}
				break;
				
			case "reportDeleteCancel":
				this._reportDeleteCancel = this.mozPrefs.getCharPref("reportDeleteCancel");
				break;

			case "storeLocally":
				this._storeLocally = this.mozPrefs.getBoolPref("storeLocally");
				break;

			case "maxLocalStorage":
				this._maxLocalStorage = this.mozPrefs.getIntPref("maxLocalStorage");
				break;
			
			case "displayStatusCount":
				this._displayStatusCount = this.mozPrefs.getIntPref("displayStatusCount");
				break;
			case "highlight3rdPartyResources":
				this._highlight3rdPartyResources = this.mozPrefs.getIntPref("highlight3rdPartyResources");
				break;

			
		}
	},
	
	get server() 
	{ 
		return this._server;
	},
	set server(value) 
	{
		this._server = value;
		this.mozPrefs.setCharPref("server", value);
	},

	get shouldRun() 
	{ 
		return this._shouldRun;
	},
	set shouldRun(value) 
	{
		this._shouldRun = value;
		this.mozPrefs.setBoolPref("shouldRun", value);
	},

	get shouldUpload() 
	{ 
		return this._shouldUpload;
	},
	set shouldUpload(value) 
	{
		this._shouldUpload = value;
		this.mozPrefs.setBoolPref("shouldUpload", value);
	},

	get googleUsername() 
	{ 
		return this._googleUsername;
	},
	set googleUsername(value) 
	{
		this._googleUsername = value;
		this.mozPrefs.setCharPref("googleUsername", value);
	},

	get uniqueID() 
	{ 
		return this._uniqueID;
	},
	set uniqueID(value) 
	{
		this._uniqueID = value;
		this.mozPrefs.setCharPref("uniqueID", value);
	},

	get reportDeleteCancel() 
	{ 
		return this._reportDeleteCancel;
	},
	set reportDeleteCancel(value) 
	{
		this._reportDeleteCancel = value;
		this.mozPrefs.setCharPref("reportDeleteCancel", value);
	},

	get storeLocally() 
	{ 
		return this._storeLocally;
	},
	set storeLocally(value) 
	{
		this._storeLocally = value;
		this.mozPrefs.setBoolPref("storeLocally", value);
	},

	get maxLocalStorage() 
	{ 
		return this._maxLocalStorage;
	},
	set maxLocalStorage(value) 
	{
		this._maxLocalStorage = value;
		this.mozPrefs.setIntPref("maxLocalStorage", value);
	},


	get displayStatusCount() 
	{ 
		return this._displayStatusCount;
	},
	set displayStatusCount(value) 
	{
		this._displayStatusCount = value;
		this.mozPrefs.setIntPref("displayStatusCount", value);
	},

	get highlight3rdPartyResources() 
	{ 
		return this._highlight3rdPartyResources;
	},
	set highlight3rdPartyResources(value) 
	{
		this._highlight3rdPartyResources = value;
		this.mozPrefs.setIntPref("highlight3rdPartyResources", value);
	},


	generateUniqueID : function(){
		function S4() {
			return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
		}
		return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
	}
	
};
